#include <iostream>
#include <pthread.h>
#include <mutex>

class Mutex // 锁对象
{
public:
    Mutex(pthread_mutex_t *pmutex) : _pmutex(pmutex)
    {
    }
    ~Mutex()
    {
    }

    void Lock()
    {
        std::cout << "mutex lock" << std::endl;
        pthread_mutex_lock(_pmutex);
    }
    void UnLock()
    {
        std::cout << "mutex unlock" << std::endl;
        pthread_mutex_unlock(_pmutex);
    }

private:
    pthread_mutex_t *_pmutex;
};

class lockGuard // RAII智能指针，对锁对象进行自动监管
{
public:
    lockGuard(pthread_mutex_t* pmtx) : _mtx(pmtx)//初始化，调用其构造函数，并赋值mtx
    {
        _mtx.Lock();
    }
    ~lockGuard()
    {
        _mtx.UnLock();
    }

private:
    Mutex _mtx;
};